﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jy123.DBUtility;
using Jy123.BLL.Public;

namespace Jy123.BLL.Annexe
{
    public class Annexe
    {
        public List<Jy123.Model.Public_Annexe> AnnexeListCanUse(Jy123.Model.Public_Level level)
        {
            Public_Annexe BLLAnnexe = new Public_Annexe();
            int JType = level.FJType.Value;
            string strWhere = string.Format("FJType = {0} AND FAType <> 6", JType);//6为广告，为所有可用
            List<Jy123.Model.Public_Annexe> listAnnexe = BLLAnnexe.GetModelList(strWhere);
            List<Jy123.Model.Public_Annexe> listResult = new List<Jy123.Model.Public_Annexe>();
            foreach (Jy123.Model.Public_Annexe annexe in listAnnexe)
            {
                if (annexe.UsingLevel.Contains(level.ID.ToString()))
                {
                    listResult.Add(annexe);
                }
            }
            return listResult;
        }

        public List<Jy123.Model.Public_Annexe> GetAListByCd(int level, int count, int type)
        {
            Public_Annexe BLLAnnexe = new Public_Annexe();
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("SELECT TOP {0} * FROM Public_Annexe WHERE ", count.ToString());
            strSql.AppendFormat("FAType = {0} AND ADLevel = {1} ORDER BY AdOrder", type.ToString(), level.ToString());
            DataSet dsResult = DbHelperSQL.Query(strSql.ToString());
            if (dsResult.Tables.Count > 0)
            {
                List<Jy123.Model.Public_Annexe> listResult = BLLAnnexe.DataTableToList(dsResult.Tables[0]);
                return listResult;
            }
            return null;
        }

        public bool BuyAnnexe(Model.Public_Annexe annexe, int moduleId, Model.User_Main user)
        {
            try
            {
                BLL.User_Main BLLUser = new User_Main();
                BLL.Annexe_Record BLLAnnexe = new Jy123.BLL.Annexe_Record();
                Model.Annexe_Record aRecord = new Jy123.Model.Annexe_Record();
                aRecord.FAnnexe = annexe.ID;
                aRecord.FModule = moduleId;
                aRecord.IsUsed = false;
                aRecord.AFather = null;
                BLLAnnexe.Add(aRecord);
                user.Cash = Convert.ToInt32(user.Cash - annexe.Price.Value);
                BLL.Module_Main BLLModule = new Module_Main();
                Model.Module_Main module = BLLModule.GetModel(moduleId);
                Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
                Jy123.Model.Public_JType pJType = BLLJType.GetModel(module.FJType.Value);
                int jFather = pJType.JFather.Value;
                if (jFather == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.JyType.Venues_Main))
                {
                    Venues_Main BLLVenues = new Venues_Main();
                    Model.Venues_Main venues = BLLVenues.GetModel(moduleId);
                    if (venues.Assets.HasValue)
                    {
                        venues.Assets = venues.Assets.Value + annexe.Price.Value;
                    }
                    else
                    {
                        venues.Assets = annexe.Price.Value;
                    }
                    BLLVenues.Update(venues);
                }
                else
                {
                    TCenter_Main BLLTCenter = new TCenter_Main();
                    Model.TCenter_Main TCenter = BLLTCenter.GetModel(moduleId);
                    if (TCenter.Assets.HasValue)
                    {
                        TCenter.Assets = TCenter.Assets.Value + annexe.Price.Value;
                    }
                    else
                    {
                        TCenter.Assets = annexe.Price.Value;
                    }
                    BLLTCenter.Update(TCenter);
                }



                BLLUser.Update(user);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
